package com.helper.mgr

import android.content.Context

import com.sh.doctors.cloud.BuildConfig
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * 页面描述：Preference
 *
 * Created by ditclear on 2017/12/14.
 */
class Preference<T>(
    val context: Context, val name: String, val
    default: T
) : ReadWriteProperty<Any?, T> {
    val prefs by lazy {
        context.getSharedPreferences("default", Context.MODE_PRIVATE)
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return findPreference(name, default)
    }

    override fun setValue(
        thisRef: Any?, property: KProperty<*>,
        value: T
    ) {
        if (BuildConfig.DEBUG) {
//            LogMgr.d("preference:$value")
        }
        putPreference(name, value)
    }

    fun <T> findPreference(name: String, default: T): T =
        with(prefs) {
            val res: Any = when (default) {
                is Long -> getLong(name, default)
                is String -> this!!.getString(name, default)!!
                is Int -> getInt(name, default)
                is Boolean -> getBoolean(name, default)
                is Float -> getFloat(name, default)
                else -> throw IllegalArgumentException(
                    "This type can be saved into Preferences"
                )
            }
            res as T
        }

    private fun <U> putPreference(name: String, value: U) =
        with(prefs.edit()) {
            when (value) {
                is Long -> putLong(name, value)
                is String -> putString(name, value)
                is Int -> putInt(name, value)
                is Boolean -> putBoolean(name, value)
                is Float -> putFloat(name, value)
                else -> throw IllegalArgumentException("This type can be saved into Preferences")
            }.commit()
        }
}